// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: "s-web-devin-3gvwv5vda1185f99" }) // 使用当前云环境

const db = cloud.database()

const fetchHomeList = async ({ page, size, _id }) => {
  const data = await // 查询数据
    db.collection('users')
      .orderBy("_id", "desc")
      .skip(1)
      .limit(10)
      .get()
  return data
}

const updateNumber = async ({ _id }) => {
  // 1. 更新数据，需要判断+/-
  // 2. 查询到这条数据，拿到最新数据，然后更新number
  const goods = await db.collection('s_2006a').doc(_id).get()
  // 3. 更新数据库
  // goods.number++;
  console.log('goods:', goods.data)
  goods.data.number++;
  // console.log('goods[0].number:', goods)
  const updateItem = await db.collection('s_2006a').doc(_id)
    .update({
      data: {
        number: goods.data.number,
      }
    })
  return updateItem
}

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const { page = 1, size = 5, _id } = event
  console.log('id_', _id)
  // const response = await fetchHomeList({ page, size })
  const response = await updateNumber({ _id })
  return {
    event,
    response
  }
}